home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / remote-eb.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  23KB  |  943 lines

  1. /* Remote debugging interface for AMD 29000 EBMON on IBM PC, for GDB.
  2.    Copyright 1990-1991 Free Software Foundation, Inc.
  3.    Contributed by Cygnus Support.  Written by Jim Kingdon for Cygnus.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* This is like remote.c but is for an esoteric situation--
  22.    having a 29k board in a PC hooked up to a unix machine with
  23.    a serial line, and running ctty com1 on the PC, through which
  24.    the unix machine can run ebmon.  Not to mention that the PC
  25.    has PC/NFS, so it can access the same executables that gdb can,
  26.    over the net in real time.  */
  27.  
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include "defs.h"
  31. #include "tm-29k.h"
  32. #include "param-no-tm.h"
  33. #include "inferior.h"
  34. #include "wait.h"
  35. #include "value.h"
  36. #include <ctype.h>
  37. #include <fcntl.h>
  38. #include <signal.h>
  39. #include <errno.h>
  40. #include "terminal.h"
  41. #include "target.h"
  42.  
  43. extern struct value *call_function_by_hand();
  44.  
  45. extern struct target_ops eb_ops;        /* Forward declaration */
  46.  
  47. static void eb_close();
  48.  
  49. #define LOG_FILE "eb.log"
  50. #if defined (LOG_FILE)
  51. FILE *log_file;
  52. #endif
  53.  
  54. static int timeout = 5;
  55.  
  56. /* Descriptor for I/O to remote machine.  Initialize it to -1 so that
  57.    eb_open knows that we don't have a file open when the program
  58.    starts.  */
  59. int eb_desc = -1;
  60.  
  61. /* stream which is fdopen'd from eb_desc.  Only valid when
  62.    eb_desc != -1.  */
  63. FILE *eb_stream;
  64.  
  65. /* Read a character from the remote system, doing all the fancy
  66.    timeout stuff.  */
  67. static int
  68. readchar ()
  69. {
  70.   char buf;
  71.  
  72.   buf = '\0';
  73. #ifdef HAVE_TERMIO
  74.   /* termio does the timeout for us.  */
  75.   read (eb_desc, &buf, 1);
  76. #else
  77.   alarm (timeout);
  78.   if (read (eb_desc, &buf, 1) < 0)
  79.     {
  80.       if (errno == EINTR)
  81.     error ("Timeout reading from remote system.");
  82.       else
  83.     perror_with_name ("remote");
  84.     }
  85.   alarm (0);
  86. #endif
  87.  
  88.   if (buf == '\0')
  89.     error ("Timeout reading from remote system.");
  90. #if defined (LOG_FILE)
  91.   putc (buf & 0x7f, log_file);
  92. #endif
  93.   return buf & 0x7f;
  94. }
  95.  
  96. /* Keep discarding input from the remote system, until STRING is found. 
  97.    Let the user break out immediately.  */
  98. static void
  99. expect (string)
  100.      char *string;
  101. {
  102.   char *p = string;
  103.  
  104.   immediate_quit = 1;
  105.   while (1)
  106.     {
  107.       if (readchar() == *p)
  108.     {
  109.       p++;
  110.       if (*p == '\0')
  111.         {
  112.           immediate_quit = 0;
  113.           return;
  114.         }
  115.     }
  116.       else
  117.     p = string;
  118.     }
  119. }
  120.  
  121. /* Keep discarding input until we see the ebmon prompt.
  122.  
  123.    The convention for dealing with the prompt is that you
  124.    o give your command
  125.    o *then* wait for the prompt.
  126.  
  127.    Thus the last thing that a procedure does with the serial line
  128.    will be an expect_prompt().  Exception:  eb_resume does not
  129.    wait for the prompt, because the terminal is being handed over
  130.    to the inferior.  However, the next thing which happens after that
  131.    is a eb_wait which does wait for the prompt.
  132.    Note that this includes abnormal exit, e.g. error().  This is
  133.    necessary to prevent getting into states from which we can't
  134.    recover.  */
  135. static void
  136. expect_prompt ()
  137. {
  138. #if defined (LOG_FILE)
  139.   /* This is a convenient place to do this.  The idea is to do it often
  140.      enough that we never lose much data if we terminate abnormally.  */
  141.   fflush (log_file);
  142. #endif
  143.   expect ("\n# ");
  144. }
  145.  
  146. /* Get a hex digit from the remote system & return its value.
  147.    If ignore_space is nonzero, ignore spaces (not newline, tab, etc).  */
  148. static int
  149. get_hex_digit (ignore_space)
  150.      int ignore_space;
  151. {
  152.   int ch;
  153.   while (1)
  154.     {
  155.       ch = readchar ();
  156.       if (ch >= '0' && ch <= '9')
  157.     return ch - '0';
  158.       else if (ch >= 'A' && ch <= 'F')
  159.     return ch - 'A' + 10;
  160.       else if (ch >= 'a' && ch <= 'f')
  161.     return ch - 'a' + 10;
  162.       else if (ch == ' ' && ignore_space)
  163.     ;
  164.       else
  165.     {
  166.       expect_prompt ();
  167.       error ("Invalid hex digit from remote system.");
  168.     }
  169.     }
  170. }
  171.  
  172. /* Get a byte from eb_desc and put it in *BYT.  Accept any number
  173.    leading spaces.  */
  174. static void
  175. get_hex_byte (byt)
  176.      char *byt;
  177. {
  178.   int val;
  179.  
  180.   val = get_hex_digit (1) << 4;
  181.   val |= get_hex_digit (0);
  182.   *byt = val;
  183. }
  184.  
  185. /* Get N 32-bit words from remote, each preceded by a space,
  186.    and put them in registers starting at REGNO.  */
  187. static void
  188. get_hex_regs (n, regno)
  189.      int n;
  190.      int regno;
  191. {
  192.   long val;
  193.   int i;
  194.  
  195.   for (i = 0; i < n; i++)
  196.     {
  197.       int j;
  198.       
  199.       val = 0;
  200.       for (j = 0; j < 8; j++)
  201.     val = (val << 4) + get_hex_digit (j == 0);
  202.       supply_register (regno++, &val);
  203.     }
  204. }
  205.  
  206. /* Called when SIGALRM signal sent due to alarm() timeout.  */
  207. #ifndef HAVE_TERMIO
  208.  
  209. #ifndef __STDC__
  210. #define volatile /**/
  211. #endif
  212. volatile int n_alarms;
  213.  
  214. void
  215. eb_timer ()
  216. {
  217. #if 0
  218.   if (kiodebug)
  219.     printf ("eb_timer called\n");
  220. #endif
  221.   n_alarms++;
  222. }
  223. #endif
  224.  
  225. /* malloc'd name of the program on the remote system.  */
  226. static char *prog_name = NULL;
  227.  
  228. /* Nonzero if we have loaded the file ("yc") and not yet issued a "gi"
  229.    command.  "gi" is supposed to happen exactly once for each "yc".  */
  230. static int need_gi = 0;
  231.  
  232. /* Number of SIGTRAPs we need to simulate.  That is, the next
  233.    NEED_ARTIFICIAL_TRAP calls to eb_wait should just return
  234.    SIGTRAP without actually waiting for anything.  */
  235.  
  236. static int need_artificial_trap = 0;
  237.  
  238. /* This is called not only when we first attach, but also when the
  239.    user types "run" after having attached.  */
  240. void
  241. eb_start (inferior_args)
  242. char *inferior_args;
  243. {
  244.   /* OK, now read in the file.  Y=read, C=COFF, D=no symbols
  245.      0=start address, %s=filename.  */
  246.  
  247.   fprintf (eb_stream, "YC D,0:%s", prog_name);
  248.  
  249.   if (inferior_args != NULL)
  250.       fprintf(eb_stream, " %s", inferior_args);
  251.  
  252.   fprintf (eb_stream, "\n");
  253.   fflush (eb_stream);
  254.  
  255.   expect_prompt ();
  256.  
  257.   need_gi = 1;
  258. }
  259.  
  260. /* Translate baud rates from integers to damn B_codes.  Unix should
  261.    have outgrown this crap years ago, but even POSIX wouldn't buck it.  */
  262.  
  263. #ifndef B19200
  264. #define B19200 EXTA
  265. #endif
  266. #ifndef B38400
  267. #define B38400 EXTB
  268. #endif
  269.  
  270. struct {int rate, damn_b;} baudtab[] = {
  271.     {0, B0},
  272.     {50, B50},
  273.     {75, B75},
  274.     {110, B110},
  275.     {134, B134},
  276.     {150, B150},
  277.     {200, B200},
  278.     {300, B300},
  279.     {600, B600},
  280.     {1200, B1200},
  281.     {1800, B1800},
  282.     {2400, B2400},
  283.     {4800, B4800},
  284.     {9600, B9600},
  285.     {19200, B19200},
  286.     {38400, B38400},
  287.     {-1, -1},
  288. };
  289.  
  290. int damn_b (rate)
  291.      int rate;
  292. {
  293.   int i;
  294.  
  295.   for (i = 0; baudtab[i].rate != -1; i++)
  296.     if (rate == baudtab[i].rate) return baudtab[i].damn_b;
  297.   return B38400;    /* Random */
  298. }
  299.  
  300.  
  301. /* Open a connection to a remote debugger.
  302.    NAME is the filename used for communication, then a space,
  303.    then the name of the program as we should name it to EBMON.  */
  304.  
  305. static int baudrate = 9600;
  306. static char *dev_name;
  307. void
  308. eb_open (name, from_tty)
  309.      char *name;
  310.      int from_tty;
  311. {
  312.   TERMINAL sg;
  313.  
  314.   char *p;
  315.  
  316.   target_preopen (from_tty);
  317.   
  318.   /* Find the first whitespace character, it separates dev_name from
  319.      prog_name.  */
  320.   if (name == 0)
  321.     goto erroid;
  322.  
  323.   for (p = name;
  324.        *p != '\0' && !isspace (*p); p++)
  325.     ;
  326.   if (*p == '\0')
  327. erroid:
  328.     error ("\
  329. Please include the name of the device for the serial port,\n\
  330. the baud rate, and the name of the program to run on the remote system.");
  331.   dev_name = alloca (p - name + 1);
  332.   strncpy (dev_name, name, p - name);
  333.   dev_name[p - name] = '\0';
  334.  
  335.   /* Skip over the whitespace after dev_name */
  336.   for (; isspace (*p); p++)
  337.     /*EMPTY*/;
  338.   
  339.   if (1 != sscanf (p, "%d ", &baudrate))
  340.     goto erroid;
  341.  
  342.   /* Skip the number and then the spaces */
  343.   for (; isdigit (*p); p++)
  344.     /*EMPTY*/;
  345.   for (; isspace (*p); p++)
  346.     /*EMPTY*/;
  347.   
  348.   if (prog_name != NULL)
  349.     free (prog_name);
  350.   prog_name = savestring (p, strlen (p));
  351.  
  352.   eb_close (0);
  353.  
  354.   eb_desc = open (dev_name, O_RDWR);
  355.   if (eb_desc < 0)
  356.     perror_with_name (dev_name);
  357.   ioctl (eb_desc, TIOCGETP, &sg);
  358. #ifdef HAVE_TERMIO
  359.   sg.c_cc[VMIN] = 0;        /* read with timeout.  */
  360.   sg.c_cc[VTIME] = timeout * 10;
  361.   sg.c_lflag &= ~(ICANON | ECHO);
  362.   sg.c_cflag = (sg.c_cflag & ~CBAUD) | damn_b (baudrate);
  363. #else
  364.   sg.sg_ispeed = damn_b (baudrate);
  365.   sg.sg_ospeed = damn_b (baudrate);
  366.   sg.sg_flags |= RAW | ANYP;
  367.   sg.sg_flags &= ~ECHO;
  368. #endif
  369.  
  370.   ioctl (eb_desc, TIOCSETP, &sg);
  371.   eb_stream = fdopen (eb_desc, "r+");
  372.  
  373.   push_target (&eb_ops);
  374.   if (from_tty)
  375.     printf ("Remote %s debugging %s using %s\n", target_shortname,
  376.         prog_name, dev_name);
  377.  
  378. #ifndef HAVE_TERMIO
  379. #ifndef NO_SIGINTERRUPT
  380.   /* Cause SIGALRM's to make reads fail with EINTR instead of resuming
  381.      the read.  */
  382.   if (siginterrupt (SIGALRM, 1) != 0)
  383.     perror ("eb_open: error in siginterrupt");
  384. #endif
  385.  
  386.   /* Set up read timeout timer.  */
  387.   if ((void (*)) signal (SIGALRM, eb_timer) == (void (*)) -1)
  388.     perror ("eb_open: error in signal");
  389. #endif
  390.  
  391. #if defined (LOG_FILE)
  392.   log_file = fopen (LOG_FILE, "w");
  393.   if (log_file == NULL)
  394.     perror_with_name (LOG_FILE);
  395. #endif
  396.  
  397.   /* Hello?  Are you there?  */
  398.   write (eb_desc, "\n", 1);
  399.   
  400.   expect_prompt ();
  401. }
  402.  
  403. /* Close out all files and local state before this target loses control. */
  404.  
  405. static void
  406. eb_close (quitting)
  407.      int quitting;
  408. {
  409.  
  410.   /* Due to a bug in Unix, fclose closes not only the stdio stream,
  411.      but also the file descriptor.  So we don't actually close
  412.      eb_desc.  */
  413.   if (eb_stream)
  414.     fclose (eb_stream);    /* This also closes eb_desc */
  415.   if (eb_desc >= 0)
  416.     /* close (eb_desc); */
  417.  
  418.   /* Do not try to close eb_desc again, later in the program.  */
  419.   eb_stream = NULL;
  420.   eb_desc = -1;
  421.  
  422. #if defined (LOG_FILE)
  423.   if (ferror (log_file))
  424.     printf ("Error writing log file.\n");
  425.   if (fclose (log_file) != 0)
  426.     printf ("Error closing log file.\n");
  427. #endif
  428. }
  429.  
  430. /* Terminate the open connection to the remote debugger.
  431.    Use this when you want to detach and do something else
  432.    with your gdb.  */
  433. void
  434. eb_detach (from_tty)
  435.      int from_tty;
  436. {
  437.   pop_target();        /* calls eb_close to do the real work */
  438.   if (from_tty)
  439.     printf ("Ending remote %s debugging\n", target_shortname);
  440. }
  441.  
  442. /* Tell the remote machine to resume.  */
  443.  
  444. void
  445. eb_resume (step, sig)
  446.      int step, sig;
  447. {
  448.   if (step)
  449.     {
  450.       write (eb_desc, "t 1,s\n", 6);
  451.       /* Wait for the echo.  */
  452.       expect ("t 1,s\r");
  453.       /* Then comes a line containing the instruction we stepped to.  */
  454.       expect ("\n@");
  455.       /* Then we get the prompt.  */
  456.       expect_prompt ();
  457.  
  458.       /* Force the next eb_wait to return a trap.  Not doing anything
  459.          about I/O from the target means that the user has to type
  460.          "continue" to see any.  This should be fixed.  */
  461.       need_artificial_trap = 1;
  462.     }
  463.   else
  464.     {
  465.       if (need_gi)
  466.     {
  467.       need_gi = 0;
  468.       write (eb_desc, "gi\n", 3);
  469.       
  470.       /* Swallow the echo of "gi".  */
  471.       expect ("gi\r");
  472.     }
  473.       else
  474.     {
  475.       write (eb_desc, "GR\n", 3);
  476.       /* Swallow the echo.  */
  477.       expect ("GR\r");
  478.     }
  479.     }
  480. }
  481.  
  482. /* Wait until the remote machine stops, then return,
  483.    storing status in STATUS just as `wait' would.  */
  484.  
  485. int
  486. eb_wait (status)
  487.      WAITTYPE *status;
  488. {
  489.   /* Strings to look for.  '?' means match any single character.  
  490.      Note that with the algorithm we use, the initial character
  491.      of the string cannot recur in the string, or we will not
  492.      find some cases of the string in the input.  */
  493.   
  494.   static char bpt[] = "Invalid interrupt taken - #0x50 - ";
  495.   /* It would be tempting to look for "\n[__exit + 0x8]\n"
  496.      but that requires loading symbols with "yc i" and even if
  497.      we did do that we don't know that the file has symbols.  */
  498.   static char exitmsg[] = "\n@????????I    JMPTI     GR121,LR0";
  499.   char *bp = bpt;
  500.   char *ep = exitmsg;
  501.  
  502.   /* Large enough for either sizeof (bpt) or sizeof (exitmsg) chars.  */
  503.   char swallowed[50];
  504.   /* Current position in swallowed.  */
  505.   char *swallowed_p = swallowed;
  506.  
  507.   int ch;
  508.   int ch_handled;
  509.  
  510.   int old_timeout = timeout;
  511.  
  512.   WSETEXIT ((*status), 0);
  513.  
  514.   if (need_artificial_trap != 0)
  515.     {
  516.       WSETSTOP ((*status), SIGTRAP);
  517.       need_artificial_trap--;
  518.       return 0;
  519.     }
  520.  
  521.   timeout = 0;        /* Don't time out -- user program is running. */
  522.   while (1)
  523.     {
  524.       ch_handled = 0;
  525.       ch = readchar ();
  526.       if (ch == *bp)
  527.     {
  528.       bp++;
  529.       if (*bp == '\0')
  530.         break;
  531.       ch_handled = 1;
  532.  
  533.       *swallowed_p++ = ch;
  534.     }
  535.       else
  536.     bp = bpt;
  537.  
  538.       if (ch == *ep || *ep == '?')
  539.     {
  540.       ep++;
  541.       if (*ep == '\0')
  542.         break;
  543.  
  544.       if (!ch_handled)
  545.         *swallowed_p++ = ch;
  546.       ch_handled = 1;
  547.     }
  548.       else
  549.     ep = exitmsg;
  550.  
  551.       if (!ch_handled)
  552.     {
  553.       char *p;
  554.  
  555.       /* Print out any characters which have been swallowed.  */
  556.       for (p = swallowed; p < swallowed_p; ++p)
  557.         putc (*p, stdout);
  558.       swallowed_p = swallowed;
  559.       
  560.       putc (ch, stdout);
  561.     }
  562.     }
  563.   expect_prompt ();
  564.   if (*bp== '\0')
  565.     WSETSTOP ((*status), SIGTRAP);
  566.   else
  567.     WSETEXIT ((*status), 0);
  568.   timeout = old_timeout;
  569.  
  570.   return 0;
  571. }
  572.  
  573. /* Return the name of register number REGNO
  574.    in the form input and output by EBMON.
  575.  
  576.    Returns a pointer to a static buffer containing the answer.  */
  577. static char *
  578. get_reg_name (regno)
  579.      int regno;
  580. {
  581.   static char buf[80];
  582.   if (regno >= GR96_REGNUM && regno < GR96_REGNUM + 32)
  583.     sprintf (buf, "GR%03d", regno - GR96_REGNUM + 96);
  584.   else if (regno >= LR0_REGNUM && regno < LR0_REGNUM + 128)
  585.     sprintf (buf, "LR%03d", regno - LR0_REGNUM);
  586.   else if (regno == Q_REGNUM)
  587.     strcpy (buf, "SR131");
  588.   else if (regno >= BP_REGNUM && regno <= CR_REGNUM)
  589.     sprintf (buf, "SR%03d", regno - BP_REGNUM + 133);
  590.   else if (regno == ALU_REGNUM)
  591.     strcpy (buf, "SR132");
  592.   else if (regno >= IPC_REGNUM && regno <= IPB_REGNUM)
  593.     sprintf (buf, "SR%03d", regno - IPC_REGNUM + 128);
  594.   else if (regno >= VAB_REGNUM && regno <= LRU_REGNUM)
  595.     sprintf (buf, "SR%03d", regno - VAB_REGNUM);
  596.   else if (regno == GR1_REGNUM)
  597.     strcpy (buf, "GR001");
  598.   return buf;
  599. }
  600.  
  601. /* Read the remote registers into the block REGS.  */
  602.  
  603. static void
  604. eb_fetch_registers ()
  605. {
  606.   int reg_index;
  607.   int regnum_index;
  608.   char tempbuf[10];
  609.   int i;
  610.  
  611. #if 0
  612.   /* This should not be necessary, because one is supposed to read the
  613.      registers only when the inferior is stopped (at least with
  614.      ptrace() and why not make it the same for remote?).  */
  615.   /* ^A is the "normal character" used to make sure we are talking to EBMON
  616.      and not to the program being debugged.  */
  617.   write (eb_desc, "\001\n");
  618.   expect_prompt ();
  619. #endif
  620.  
  621.   write (eb_desc, "dw gr96,gr127\n", 14);
  622.   for (reg_index = 96, regnum_index = GR96_REGNUM;
  623.        reg_index < 128;
  624.        reg_index += 4, regnum_index += 4)
  625.     {
  626.       sprintf (tempbuf, "GR%03d ", reg_index);
  627.       expect (tempbuf);
  628.       get_hex_regs (4, regnum_index);
  629.       expect ("\n");
  630.     }
  631.  
  632.   for (i = 0; i < 128; i += 32)
  633.     {
  634.       /* The PC has a tendency to hang if we get these
  635.      all in one fell swoop ("dw lr0,lr127").  */
  636.       sprintf (tempbuf, "dw lr%d\n", i);
  637.       write (eb_desc, tempbuf, strlen (tempbuf));
  638.       for (reg_index = i, regnum_index = LR0_REGNUM + i;
  639.        reg_index < i + 32;
  640.        reg_index += 4, regnum_index += 4)
  641.     {
  642.       sprintf (tempbuf, "LR%03d ", reg_index);
  643.       expect (tempbuf);
  644.       get_hex_regs (4, regnum_index);
  645.       expect ("\n");
  646.     }
  647.     }
  648.  
  649.   write (eb_desc, "dw sr133,sr133\n", 15);
  650.   expect ("SR133          ");
  651.   get_hex_regs (1, BP_REGNUM);
  652.   expect ("\n");
  653.  
  654.   write (eb_desc, "dw sr134,sr134\n", 15);
  655.   expect ("SR134                   ");
  656.   get_hex_regs (1, FC_REGNUM);
  657.   expect ("\n");
  658.  
  659.   write (eb_desc, "dw sr135,sr135\n", 15);
  660.   expect ("SR135                            ");
  661.   get_hex_regs (1, CR_REGNUM);
  662.   expect ("\n");
  663.  
  664.   write (eb_desc, "dw sr131,sr131\n", 15);
  665.   expect ("SR131                            ");
  666.   get_hex_regs (1, Q_REGNUM);
  667.   expect ("\n");
  668.  
  669.   write (eb_desc, "dw sr0,sr14\n", 12);
  670.   for (reg_index = 0, regnum_index = VAB_REGNUM;
  671.        regnum_index <= LRU_REGNUM;
  672.        regnum_index += 4, reg_index += 4)
  673.     {
  674.       sprintf (tempbuf, "SR%03d ", reg_index);
  675.       expect (tempbuf);
  676.       get_hex_regs (reg_index == 12 ? 3 : 4, regnum_index);
  677.       expect ("\n");
  678.     }
  679.  
  680.   /* There doesn't seem to be any way to get these.  */
  681.   {
  682.     int val = -1;
  683.     supply_register (FPE_REGNUM, &val);
  684.     supply_register (INT_REGNUM, &val);
  685.     supply_register (FPS_REGNUM, &val);
  686.     supply_register (EXO_REGNUM, &val);
  687.   }
  688.  
  689.   write (eb_desc, "dw gr1,gr1\n", 11);
  690.   expect ("GR001 ");
  691.   get_hex_regs (1, GR1_REGNUM);
  692.   expect_prompt ();
  693. }
  694.  
  695. /* Fetch register REGNO, or all registers if REGNO is -1.
  696.    Returns errno value.  */
  697. void
  698. eb_fetch_register (regno)
  699.      int regno;
  700. {
  701.   if (regno == -1)
  702.     eb_fetch_registers ();
  703.   else
  704.     {
  705.       char *name = get_reg_name (regno);
  706.       fprintf (eb_stream, "dw %s,%s\n", name, name);
  707.       expect (name);
  708.       expect (" ");
  709.       get_hex_regs (1, regno);
  710.       expect_prompt ();
  711.     }
  712.   return;
  713. }
  714.  
  715. /* Store the remote registers from the contents of the block REGS.  */
  716.  
  717. static void
  718. eb_store_registers ()
  719. {
  720.   int i, j;
  721.   fprintf (eb_stream, "s gr1,%x\n", read_register (GR1_REGNUM));
  722.   expect_prompt ();
  723.  
  724.   for (j = 0; j < 32; j += 16)
  725.     {
  726.       fprintf (eb_stream, "s gr%d,", j + 96);
  727.       for (i = 0; i < 15; ++i)
  728.     fprintf (eb_stream, "%x,", read_register (GR96_REGNUM + j + i));
  729.       fprintf (eb_stream, "%x\n", read_register (GR96_REGNUM + j + 15));
  730.       expect_prompt ();
  731.     }
  732.  
  733.   for (j = 0; j < 128; j += 16)
  734.     {
  735.       fprintf (eb_stream, "s lr%d,", j);
  736.       for (i = 0; i < 15; ++i)
  737.     fprintf (eb_stream, "%x,", read_register (LR0_REGNUM + j + i));
  738.       fprintf (eb_stream, "%x\n", read_register (LR0_REGNUM + j + 15));
  739.       expect_prompt ();
  740.     }
  741.  
  742.   fprintf (eb_stream, "s sr133,%x,%x,%x\n", read_register (BP_REGNUM),
  743.        read_register (FC_REGNUM), read_register (CR_REGNUM));
  744.   expect_prompt ();
  745.   fprintf (eb_stream, "s sr131,%x\n", read_register (Q_REGNUM));
  746.   expect_prompt ();
  747.   fprintf (eb_stream, "s sr0,");
  748.   for (i = 0; i < 11; ++i)
  749.     fprintf (eb_stream, "%x,", read_register (VAB_REGNUM + i));
  750.   fprintf (eb_stream, "%x\n", read_register (VAB_REGNUM + 11));
  751.   expect_prompt ();
  752. }
  753.  
  754. /* Store register REGNO, or all if REGNO == 0.
  755.    Return errno value.  */
  756. int
  757. eb_store_register (regno)
  758.      int regno;
  759. {
  760.   if (regno == -1)
  761.     eb_store_registers ();
  762.   else
  763.     {
  764.       char *name = get_reg_name (regno);
  765.       fprintf (eb_stream, "s %s,%x\n", name, read_register (regno));
  766.       /* Setting GR1 changes the numbers of all the locals, so
  767.      invalidate the register cache.  Do this *after* calling
  768.      read_register, because we want read_register to return the
  769.      value that write_register has just stuffed into the registers
  770.      array, not the value of the register fetched from the
  771.      inferior.  */
  772.       if (regno == GR1_REGNUM)
  773.     registers_changed ();
  774.       expect_prompt ();
  775.     }
  776.   return 0;
  777. }
  778.  
  779. /* Get ready to modify the registers array.  On machines which store
  780.    individual registers, this doesn't need to do anything.  On machines
  781.    which store all the registers in one fell swoop, this makes sure
  782.    that registers contains all the registers from the program being
  783.    debugged.  */
  784.  
  785. void
  786. eb_prepare_to_store ()
  787. {
  788.   /* Do nothing, since we can store individual regs */
  789. }
  790.  
  791. /* FIXME!  Merge these two.  */
  792. int
  793. eb_xfer_inferior_memory (memaddr, myaddr, len, write, target)
  794.      CORE_ADDR memaddr;
  795.      char *myaddr;
  796.      int len;
  797.      int write;
  798.      struct target_ops *target;        /* ignored */
  799. {
  800.   if (write)
  801.     return eb_write_inferior_memory (memaddr, myaddr, len);
  802.   else
  803.     return eb_write_inferior_memory (memaddr, myaddr, len);
  804. }
  805.  
  806. void
  807. eb_files_info ()
  808. {
  809.   printf ("\tAttached to %s at %d baud and running program %s.\n",
  810.       dev_name, baudrate, prog_name);
  811. }
  812.  
  813. /* Copy LEN bytes of data from debugger memory at MYADDR
  814.    to inferior's memory at MEMADDR.  Returns errno value.  */
  815. int
  816. eb_write_inferior_memory (memaddr, myaddr, len)
  817.      CORE_ADDR memaddr;
  818.      char *myaddr;
  819.      int len;
  820. {
  821.   int i;
  822.  
  823.   for (i = 0; i < len; i++)
  824.     {
  825.       if ((i % 16) == 0)
  826.     fprintf (eb_stream, "sb %x,", memaddr + i);
  827.       if ((i % 16) == 15 || i == len - 1)
  828.     {
  829.       fprintf (eb_stream, "%x\n", ((unsigned char *)myaddr)[i]);
  830.       expect_prompt ();
  831.     }
  832.       else
  833.     fprintf (eb_stream, "%x,", ((unsigned char *)myaddr)[i]);
  834.     }
  835.   return 0;
  836. }
  837.  
  838. /* Read LEN bytes from inferior memory at MEMADDR.  Put the result
  839.    at debugger address MYADDR.  Returns errno value.  */
  840. int
  841. eb_read_inferior_memory(memaddr, myaddr, len)
  842.      CORE_ADDR memaddr;
  843.      char *myaddr;
  844.      int len;
  845. {
  846.   int i;
  847.  
  848.   /* Number of bytes read so far.  */
  849.   int count;
  850.  
  851.   /* Starting address of this pass.  */
  852.   unsigned long startaddr;
  853.  
  854.   /* Number of bytes to read in this pass.  */
  855.   int len_this_pass;
  856.  
  857.   /* Note that this code works correctly if startaddr is just less
  858.      than UINT_MAX (well, really CORE_ADDR_MAX if there was such a
  859.      thing).  That is, something like
  860.      eb_read_bytes (CORE_ADDR_MAX - 4, foo, 4)
  861.      works--it never adds len to memaddr and gets 0.  */
  862.   /* However, something like
  863.      eb_read_bytes (CORE_ADDR_MAX - 3, foo, 4)
  864.      doesn't need to work.  Detect it and give up if there's an attempt
  865.      to do that.  */
  866.   if (((memaddr - 1) + len) < memaddr)
  867.     return EIO;
  868.   
  869.   startaddr = memaddr;
  870.   count = 0;
  871.   while (count < len)
  872.     {
  873.       len_this_pass = 16;
  874.       if ((startaddr % 16) != 0)
  875.     len_this_pass -= startaddr % 16;
  876.       if (len_this_pass > (len - count))
  877.     len_this_pass = (len - count);
  878.  
  879.       fprintf (eb_stream, "db %x,%x\n", startaddr,
  880.            (startaddr - 1) + len_this_pass);
  881.       expect ("\n");
  882.  
  883.       /* Look for 8 hex digits.  */
  884.       i = 0;
  885.       while (1)
  886.     {
  887.       if (isxdigit (readchar ()))
  888.         ++i;
  889.       else
  890.         {
  891.           expect_prompt ();
  892.           error ("Hex digit expected from remote system.");
  893.         }
  894.       if (i >= 8)
  895.         break;
  896.     }
  897.  
  898.       expect ("  ");
  899.  
  900.       for (i = 0; i < len_this_pass; i++)
  901.     get_hex_byte (&myaddr[count++]);
  902.  
  903.       expect_prompt ();
  904.  
  905.       startaddr += len_this_pass;
  906.     }
  907.   return 0;
  908. }
  909.  
  910. /* Define the target subroutine names */
  911.  
  912. struct target_ops eb_ops = {
  913.     "amd-eb", "Remote serial AMD EBMON target",
  914.     "Use a remote computer running EBMON connected by a serial line.\n\
  915. Arguments are the name of the device for the serial line,\n\
  916. the speed to connect at in bits per second, and the filename of the\n\
  917. executable as it exists on the remote computer.  For example,\n\
  918.         target amd-eb /dev/ttya 9600 demo",
  919.     eb_open, eb_close, 
  920.     0, eb_detach, eb_resume, eb_wait,
  921.     eb_fetch_register, eb_store_register,
  922.     eb_prepare_to_store, 0, 0,     /* conv_to, conv_from */
  923.     eb_xfer_inferior_memory, eb_files_info,
  924.     0, 0,    /* Breakpoints */
  925.     0, 0, 0, 0, 0,    /* Terminal handling */
  926.     0,     /* FIXME, kill */
  927.     0,    /* load */
  928.     call_function_by_hand,
  929.     0, /* lookup_symbol */
  930.     0, /* create_inferior FIXME, eb_start here or something? */
  931.     0, /* mourn_inferior FIXME */
  932.     process_stratum, 0, /* next */
  933.     1, 1, 1, 1, 1,    /* all mem, mem, stack, regs, exec */
  934.     0, 0,            /* Section pointers */
  935.     OPS_MAGIC,        /* Always the last thing */
  936. };
  937.  
  938. void
  939. _initialize_remote_eb ()
  940. {
  941.   add_target (&eb_ops);
  942. }
  943.